package com.zsrd.self.controller;


import com.aliyun.oss.OSS;
import com.aliyun.oss.model.GeneratePresignedUrlRequest;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zsrd.self.base.Result;
import com.zsrd.self.base.config.AliyunOSSConfiguration;
import com.zsrd.self.entity.SelfCurr;
import com.zsrd.self.entity.SelfCurrType;
import com.zsrd.self.entity.SelfWxConfig;
import com.zsrd.self.service.SelfCurrService;
import com.zsrd.self.service.SelfCurrTypeService;
import com.zsrd.self.service.SelfWxConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Xxx
 * @since 2024-04-01
 */
@RestController
@RequestMapping("/selfCurr")
public class SelfCurrController {

    @Autowired
    private SelfCurrService selfCurrService;

    @Autowired
    SelfWxConfigService selfWxConfigService;
    @PostMapping("/currList")
    public Result currList(@RequestBody SelfCurr selfCurr){
        List<SelfCurr> list = new ArrayList<>();
        if(!StringUtils.isEmpty(selfCurr.getCurrType())){
            list = selfCurrService.list(new QueryWrapper<SelfCurr>().eq("curr_type", selfCurr.getCurrType()).eq("is_delete",0).orderByAsc("sort"));
        }

        return Result.success(list);
    }




    @PostMapping("/getUrl")
    public Result getUrl(@RequestBody SelfCurr selfCurr){
        LambdaQueryWrapper<SelfWxConfig> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SelfWxConfig::getTagType,"3");
        wrapper.eq(SelfWxConfig::getOperFlag,"oss");
        SelfWxConfig one = selfWxConfigService.getOne(wrapper);


        // 创建阿里云 OSS 客户端
        OSS ossClient = AliyunOSSConfiguration.createOSSClient(one.getAccessTokenUrl(),one.getAppId(),one.getAppSecret());
//        OSS ossClient = AliyunOSSConfiguration.createOSSClient();

        String bucketName = one.getPostUrl();
//        String bucketName = "sunxintest";

        try {
            // 配置私有文件的 key
            String objectKey = selfCurr.getUrl();
            // 生成临时访问 URL
            GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(bucketName, objectKey);
            // 设置过期时间为1小时
            request.setExpiration(new Date(System.currentTimeMillis() + 3600 * 1000));
//            request.setExpiration(new Date(System.currentTimeMillis() + 10 * 1000));
            String url = ossClient.generatePresignedUrl(request).toString();
            return Result.success(url);
        } catch (Exception e) {
            System.err.println("Failed to generate temporary URL: " + e.getMessage());
        } finally {
            // 关闭 OSS 客户端
            ossClient.shutdown();
        }
        return null;
    }


}

